package ylg.com.cn.beuty.main.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;


import java.util.List;

import ylg.com.cn.beuty.utils.ImageUtils;

/**
 *  @author miss
 */
public class NoticePagerAdapter extends PagerAdapter {
	private Context context;
	private List<String> url;
	private int[] imgeIds;
	private int flag = 0 ;//从网络上获取的图片资源,
	//从网络上获取图片;
	public NoticePagerAdapter(Context context, List<String> url) {
		flag = 0 ;
		this.context = context;
		this.url=url;
	};

	//从本地获取图片;
	public NoticePagerAdapter(Context context,int[] imgeIds){
		flag = 1;
		this.context = context;
		this.imgeIds=imgeIds;
	}

	@Override
	public int getCount() {
		if (flag==1){
			return imgeIds.length;
		}else{
			return url.size();
		}

	}

	@Override
	public Object instantiateItem(View convertView, int position) {
		
		PicModelHodler  holder = new PicModelHodler();
		holder.imageView.setScaleType(ScaleType.CENTER_CROP);
		if (flag==0){
			ImageUtils.LoadPicture(url.get(position), holder.imageView);
		}else{
			Drawable dhome = ContextCompat.getDrawable(context, imgeIds[position]);
			holder.imageView.setImageDrawable(dhome);
		}
		((ViewPager) convertView).addView(holder.imageView);
		return holder.imageView;
	}

	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		ImageView image = (ImageView) arg2;
		image.setImageBitmap(null);
		((ViewPager) arg0).removeView(image);
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}
	private final class PicModelHodler {
		public ImageView imageView = new ImageView(context);
	}

}